<template>
  <view class="privacy-settings-page">
    <!-- 顶部导航栏 -->
    <view class="header-bar">
      <view class="header-left">
        <button class="back-btn" @click="goBack">← 返回</button>
      </view>
      <view class="header-title">隐私设置</view>
      <view class="header-right"></view>
    </view>

    <!-- 设置内容 -->
    <view class="settings-content">
      <view class="settings-section">
        <view class="section-title">搜索可见性</view>
        <view class="section-desc">控制其他用户是否可以通过以下方式搜索到您</view>
        
        <view class="setting-item">
          <view class="setting-info">
            <view class="setting-label">昵称搜索</view>
            <view class="setting-desc">允许其他用户通过昵称搜索到您</view>
          </view>
          <switch 
            :checked="privacySettings.searchableByNickname" 
            @change="onSettingChange('searchableByNickname', $event.detail.value)"
            color="#4A90E2"
          />
        </view>
        
        <view class="setting-item">
          <view class="setting-info">
            <view class="setting-label">UID搜索</view>
            <view class="setting-desc">允许其他用户通过用户ID搜索到您</view>
          </view>
          <switch 
            :checked="privacySettings.searchableByUid" 
            @change="onSettingChange('searchableByUid', $event.detail.value)"
            color="#4A90E2"
          />
        </view>
        
        <view class="setting-item">
          <view class="setting-info">
            <view class="setting-label">手机号搜索</view>
            <view class="setting-desc">允许其他用户通过手机号搜索到您</view>
          </view>
          <switch 
            :checked="privacySettings.searchableByPhone" 
            @change="onSettingChange('searchableByPhone', $event.detail.value)"
            color="#4A90E2"
          />
        </view>
        
        <view class="setting-item">
          <view class="setting-info">
            <view class="setting-label">邮箱搜索</view>
            <view class="setting-desc">允许其他用户通过邮箱搜索到您</view>
          </view>
          <switch 
            :checked="privacySettings.searchableByEmail" 
            @change="onSettingChange('searchableByEmail', $event.detail.value)"
            color="#4A90E2"
          />
        </view>
        
        <view class="setting-item">
          <view class="setting-info">
            <view class="setting-label">真实姓名搜索</view>
            <view class="setting-desc">允许其他用户通过真实姓名搜索到您</view>
          </view>
          <switch 
            :checked="privacySettings.searchableByName" 
            @change="onSettingChange('searchableByName', $event.detail.value)"
            color="#4A90E2"
          />
        </view>
        
        <view class="setting-item">
          <view class="setting-info">
            <view class="setting-label">身份证号搜索</view>
            <view class="setting-desc">允许其他用户通过身份证号搜索到您</view>
          </view>
          <switch 
            :checked="privacySettings.searchableByIdCard" 
            @change="onSettingChange('searchableByIdCard', $event.detail.value)"
            color="#4A90E2"
          />
        </view>
      </view>
      
      <view class="privacy-tips">
        <view class="tips-title">🔒 隐私提示</view>
        <view class="tips-content">
          <text>• 关闭搜索可见性后，其他用户将无法通过对应方式搜索到您</text>
          <text>• 建议保持昵称和UID搜索开启，以便朋友能够找到您</text>
          <text>• 敏感信息（如手机号、身份证号）建议关闭搜索可见性</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { NETWORK_CONFIG } from '@/config/network.js'
import { ref, onMounted } from 'vue'

// 数据
const loading = ref(false)
const privacySettings = ref({
  searchableByNickname: true,
  searchableByUid: true,
  searchableByPhone: false,
  searchableByEmail: false,
  searchableByName: false,
  searchableByIdCard: false
})

// 返回上一页
const goBack = () => {
  uni.navigateBack()
}

// 加载隐私设置
const loadPrivacySettings = async () => {
  loading.value = true
  try {
    const token = uni.getStorageSync('accessToken')
    if (!token) {
      uni.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return
    }

    const response = await uni.request({
      url: `${NETWORK_CONFIG.API_BASE_URL}/user/privacy-settings`,
      method: 'GET',
      header: {
        'Access-Token': `Bearer ${token}`
      }
    })

    if (response.data) {
      privacySettings.value = response.data
    }
  } catch (error) {
    console.error('加载隐私设置失败:', error)
    uni.showToast({
      title: '加载设置失败',
      icon: 'none'
    })
  } finally {
    loading.value = false
  }
}

// 设置变更处理
const onSettingChange = async (settingKey, value) => {
  try {
    const token = uni.getStorageSync('accessToken')
    if (!token) {
      uni.showToast({
        title: '请先登录',
        icon: 'none'
      })
      return
    }

    // 立即更新UI
    privacySettings.value[settingKey] = value

    const response = await uni.request({
      url: `${NETWORK_CONFIG.API_BASE_URL}/user/privacy-settings`,
      method: 'POST',
      header: {
        'Access-Token': `Bearer ${token}`
      },
      data: {
        [settingKey]: value
      }
    })

    if (response.data) {
      uni.showToast({
        title: '设置已保存',
        icon: 'success'
      })
    } else {
      // 如果保存失败，恢复原值
      privacySettings.value[settingKey] = !value
      uni.showToast({
        title: '保存失败',
        icon: 'none'
      })
    }
  } catch (error) {
    console.error('保存隐私设置失败:', error)
    // 恢复原值
    privacySettings.value[settingKey] = !value
    uni.showToast({
      title: '保存失败',
      icon: 'none'
    })
  }
}

// 初始化
onMounted(() => {
  loadPrivacySettings()
})
</script>

<style scoped>
.privacy-settings-page {
  background: #f5f5f5;
  min-height: 100vh;
}

.header-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: #4A90E2;
  color: white;
}

.header-left, .header-right {
  flex: 1;
}

.header-title {
  flex: 2;
  text-align: center;
  font-size: 18px;
  font-weight: bold;
}

.back-btn {
  background: none;
  border: none;
  color: white;
  font-size: 16px;
  padding: 8px 12px;
  border-radius: 4px;
}

.back-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

.settings-content {
  padding: 16px;
}

.settings-section {
  background: white;
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.section-title {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  margin-bottom: 8px;
}

.section-desc {
  font-size: 14px;
  color: #666;
  margin-bottom: 20px;
  line-height: 1.4;
}

.setting-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 0;
  border-bottom: 1px solid #f0f0f0;
}

.setting-item:last-child {
  border-bottom: none;
}

.setting-info {
  flex: 1;
  margin-right: 16px;
}

.setting-label {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin-bottom: 4px;
}

.setting-desc {
  font-size: 12px;
  color: #999;
  line-height: 1.4;
}

.privacy-tips {
  background: #fff8e1;
  border: 1px solid #ffcc02;
  border-radius: 12px;
  padding: 16px;
}

.tips-title {
  font-size: 16px;
  font-weight: bold;
  color: #f57c00;
  margin-bottom: 12px;
}

.tips-content {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.tips-content text {
  font-size: 14px;
  color: #666;
  line-height: 1.4;
}
</style>
